package jedis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

/**
 * 
 * @ClassName: RedisWeiBo
 * @Description:Redis微博
 * @author 孟凡岭
 * @date 2015年8月17日 下午2:34:42
 */
public class RedisWeiBo {
	/**
	 * 获取实例
	 */
	private static Jedis jedis = new Jedis("192.168.220.128", 6379);

	public static void main(String[] args) throws Exception {
		// putMsg();
		readMsg();
	}

	/**
	 * 
	 * @Title: putMsg
	 * @Description: 放入消息
	 * @author 孟凡岭
	 * @param 参数
	 * @return void 返回类型
	 * @throws
	 */
	public static void putMsg() {
		// 会话的发起者
		Map<String, String> poster = new HashMap<String, String>();
		poster.put("userid", "user001");
		poster.put("userName", "Lily");
		poster.put("infoSeqNo", "INFO20150815");
		poster.put("postTime", "2015-08-15 20:26:30");
		poster.put("info", "今天很开心");
		poster.put("infoId", "info001");

		Map<String, String> respondent1 = new HashMap<String, String>();
		respondent1.put("userid", "user002");
		respondent1.put("userName", "Tom");
		respondent1.put("infoSeqNo", "INFO20150815");
		respondent1.put("answerTime", "2015-08-15 20:28:40");
		respondent1.put("info", "是吗");
		respondent1.put("replyTarget", "user001");
		respondent1.put("infoId", "info002");

		Map<String, String> respondent2 = new HashMap<String, String>();
		respondent2.put("userid", "user003");
		respondent2.put("userName", "Kay");
		respondent2.put("infoSeqNo", "INFO20150815");
		respondent2.put("answerTime", "2015-08-15 20:30:27");
		respondent2.put("info", "她肯定很开心啦，因为……");
		respondent2.put("replyTarget", "user002");
		respondent2.put("infoId", "info003");

		Map<String, String> respondent3 = new HashMap<String, String>();
		respondent3.put("userid", "user001");
		respondent3.put("userName", "Lily");
		respondent3.put("infoSeqNo", "INFO20150815");
		respondent3.put("answerTime", "2015-08-15 20:38:40");
		respondent3.put("info", "恩，今天去玩了");
		respondent3.put("replyTarget", "user003");
		respondent3.put("infoId", "info004");

		// 以会话流水号以及消息id作为key，消息map作为value
		jedis.hmset(
				(String) respondent1.get("infoSeqNo")
						+ respondent1.get("infoId"), respondent1);
		jedis.hmset(
				(String) respondent2.get("infoSeqNo")
						+ respondent2.get("infoId"), respondent2);
		jedis.hmset(
				(String) respondent3.get("infoSeqNo")
						+ respondent3.get("infoId"), respondent3);
		jedis.hmset((String) poster.get("infoSeqNo") + poster.get("infoId"),
				poster);
	}

	/**
	 * 
	 * @Title: readMsg
	 * @Description:读取消息
	 * @author 孟凡岭
	 * @param @throws Exception 参数
	 * @return void 返回类型
	 * @throws
	 */
	public static void readMsg() throws Exception {
		String infoSeqNo = "INFO20150815";
		Set<String> keys = jedis.keys(infoSeqNo + "*");
		List<String> keyss = new ArrayList<String>(keys);

		String postTime = "";
		List<String> times = new ArrayList<String>();// 存放所有时间(发帖以及回复)的集合
		Map<String, Map> msg = new HashMap<String, Map>();
		Map<String, String> target = new HashMap<String, String>();
		List<String> routes = new ArrayList<String>();

		for (String key : keys) {
			Map dto = jedis.hgetAll(key);
			if (dto.containsKey("postTime")) {// 表明是会话的发起者
				postTime = jedis.hget(key, "postTime");
				// times.add(postTime);
				String userName = jedis.hget(key, "userName");
				keyss.remove(key);

				target.put(jedis.hget(key, "userid"),
						jedis.hget(key, "username"));
				routes.add("【" + postTime + "】 " + userName + ": "
						+ jedis.hget(key, "info"));
				// System.out.println("【" + postTime + "】 " + userName + ": " +
				// msg);
			} else {
				String answerTime = jedis.hget(key, "answerTime");
				target.put(jedis.hget(key, "userid"),
						jedis.hget(key, "userName"));
				msg.put(answerTime, jedis.hgetAll(key));
				times.add(answerTime);
			}
		}

		int size = keyss.size();

		String time = "";
		for (int i = 0; i < size; i++) {
			String answerTime = "";
			if (i == 0) {
				answerTime = getCloseTime(postTime, times);
			} else {
				answerTime = getCloseTime(time, times);
			}
			time = getCloseTime(answerTime, times);
			Map info = msg.get(answerTime);
			String username = (String) info.get("userName");
			String targetUser = target.get(info.get("replyTarget"));
			routes.add("【" + answerTime + "】 " + username + " 回复 " + targetUser
					+ " :" + info.get("info"));
			times.remove(answerTime);
		}

		for (String str : routes) {
			System.out.println(str);
		}
		jedis.close();
	}

	/**
	 * 
	 * @Title: getCloseTime
	 * @Description: 找到距离给定时间最相近的时间
	 * @author 孟凡岭
	 * @param @param time
	 * @param @param times
	 * @param @return
	 * @param @throws Exception 参数
	 * @return String 返回类型
	 * @throws
	 */
	public static String getCloseTime(String time, List<String> times)
			throws Exception {
		String df = "yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat sd = new SimpleDateFormat(df);
		String closeTime = times.get(0);
		Date date = sd.parse(time);// 具体方法略
		Date dateTmp = sd.parse(times.get(0));
		double dif = Math.abs(date.getTime() - dateTmp.getTime());
		for (int i = 1; i < times.size(); i++) {
			Date d = sd.parse(times.get(i));
			double diff = Math.abs(date.getTime() - d.getTime());
			if (diff <= dif) {
				closeTime = times.get(i);
			}
			dif = diff;
		}
		return closeTime;
	}
}
